package com.onLineCar.management.controller;

import com.onLineCar.common.core.domain.Result;
import com.onLineCar.management.domain.RequestOrder;
import com.onLineCar.management.domain.VehicleLevel;
import com.onLineCar.management.domain.request.ContractRequest;
import com.onLineCar.management.domain.request.OrderRequest;
import com.onLineCar.management.domain.request.RequestOrders;
import com.onLineCar.management.service.PlaceAnOrderService;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author : 段敏强
 * @className : PlaceAnOrderCpntroller
 * @description : 描述说明该类的功能
 * @createTime : 2023/12/18 19:45
 */

/**
 * 订单管理 控制器
 * @author 段敏强
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class PlaceAnOrderController {

    @Autowired
    private PlaceAnOrderService placeAnOrderService;

    @Autowired
    private HttpServletRequest request;



    /**
     * 获取订单编号
     */

    @PostMapping("/orderNumber")
    public Result<String> getOrderNumber(){
        log.info("获取订单编号");
        String orderNumber = placeAnOrderService.getOrderNumber();
        return Result.success(orderNumber);
    }

    /**
     * 下单接口
     */
    @PostMapping("/orderInterface")
    public Result orderInterface(@RequestBody OrderRequest orderRequest){

        log.info("功能名称:{下单接口},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), orderRequest);
        String flag = placeAnOrderService.orderInterface(orderRequest);
            if (null == flag){
                Result.error("您有一笔行程未支付,请支付后尝试下单!”");
            }
            log.info("下单接口返回结果:{}",flag);
        return Result.success(flag,"下单成功");
    }


    /**
     * 查询所有车型
     */
    @PostMapping("/queryCarLevelAll")
    public Result queryCarLevelAll(){
        log.info("查询所有车型");
        List<VehicleLevel> list = placeAnOrderService.queryCarLevelAll();
        log.info("查询所有车型返回结果:{}",list);
        return Result.success(list);
    }

    /**
     * 根据用户id查询订单
     */
    @GetMapping("/getSelectByUserId/{id}")
    public Result<RequestOrder> getSelectByUserId(@PathVariable String id){
        log.info("根据用户id查询订单");
        RequestOrder tblOrder = placeAnOrderService.getSelectByUserId(id);
        log.info("根据用户id查询订单返回结果:{}",tblOrder);
        return Result.success(tblOrder);
    }


    /**
     * 合同签订
     */
    @PostMapping("/contract")
    public void contract(@RequestBody ContractRequest contractRequest){
        placeAnOrderService.contract(contractRequest);
    }


    /**
     * 更新用户到达状态
     */
    @GetMapping("/updateUserReach/{orderNumber}")
    public void updateUserReach(@PathVariable String orderNumber){
        placeAnOrderService.updateUserReach(orderNumber);
    }



    /**
     * 取消订单
     */
    @PostMapping("/getCancelOrder")
    public Result  cancelAnOrder(@RequestBody RequestOrders tblOrder){
        placeAnOrderService.cancelAnOrder(tblOrder);
        return Result.success();
    }

    @PostMapping("/success")
    public ModelAndView success(String out_trade_no, Double total_amount){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("success");
        modelAndView.addObject("money",total_amount);
        modelAndView.addObject("no",out_trade_no);

        System.out.println(modelAndView);
        return modelAndView;
    }


}
